package com.softeem.music_sys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Collection;
import com.softeem.music_sys.service.CollectionService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 收藏控制器
 */
@Tag(name = "收藏接口", description = "收藏相关操作:收藏、取消收藏、查询等")
@RestController
@RequestMapping("/collection")
public class CollectionController {
    
    @Autowired
    private CollectionService collectionService;

    @Operation(summary = "添加收藏", description = "收藏音乐、歌单或专辑")
    @PostMapping
    public Result add(@RequestBody Collection collection) {
        return collectionService.addCollection(collection);
    }

    @Operation(summary = "取消收藏", description = "取消收藏")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID"),
            @Parameter(name = "type", description = "收藏对象类型"),
            @Parameter(name = "targetId", description = "收藏对象ID")
    })
    @DeleteMapping
    public Result cancel(@RequestParam Integer userId, 
                        @RequestParam Object type, 
                        @RequestParam Integer targetId) {
        boolean result = collectionService.cancelCollection(userId, type, targetId);
        return result ? Result.success().setMessage("取消收藏成功") : Result.fail("取消收藏失败");
    }

    @Operation(summary = "删除收藏记录", description = "根据ID删除收藏记录")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return collectionService.deleteCollection(id);
    }

    @Operation(summary = "获取用户收藏", description = "获取用户的所有收藏")
    @GetMapping("/user/{userId}")
    public Result getByUser(@PathVariable Integer userId,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit) {
        return collectionService.getCollectionsByUserController(userId, page, limit);
    }

    @Operation(summary = "按类型获取用户收藏", description = "获取用户特定类型的收藏")
    @GetMapping("/user/{userId}/type")
    public Result getByUserAndType(@PathVariable Integer userId,
                                   @RequestParam Object type,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer limit) {
        return collectionService.getCollectionsByUserAndTypeController(userId, type, page, limit);
    }

    @Operation(summary = "检查是否收藏", description = "检查用户是否收藏了某个对象")
    @GetMapping("/check")
    public Result checkCollection(@RequestParam Integer userId,
                                  @RequestParam Object type,
                                  @RequestParam Integer targetId) {
        return collectionService.checkCollectionController(userId, type, targetId);
    }

    @Operation(summary = "获取对象的收藏数", description = "获取某个对象被收藏的次数")
    @GetMapping("/count")
    public Result getCollectionCount(@RequestParam Object type,
                                     @RequestParam Integer targetId) {
        return collectionService.getCollectionCountController(type, targetId);
    }

    @Operation(summary = "批量收藏", description = "批量收藏多个对象")
    @PostMapping("/batch")
    public Result batchAdd(@RequestParam Integer userId,
                          @RequestParam Object type,
                          @RequestBody List<Integer> targetIds) {
        return collectionService.batchAddCollection(userId, type, targetIds);
    }
}